<?php
/*
 * File name : DisplayAgency.php
 * Description : Displays the result of Agency Search
 * Created By : Pratibha Awasthi
 */
include_once "basePage.php";

class DisplayAgency extends basePage{
	
	// Display all the agency for a given Zip Code entered by the Donor.
	public function display_by_zip(){
			
			echo "<h2> List of Agency By Zip Code </h2>";
			$con = mysql_connect('localhost','pa_user','secret_password');
			if (!$con){
		  		die('Could not connect: ' . mysql_error());
		  	}
		  
			mysql_select_db("TGS_GM_RESERVATION", $con);
		
			$zip = $_POST['zipcode'];
		
			$result = mysql_query("SELECT distinct ID, Name FROM TGS_GM_RESERVATION.tblagency where Zip = {$zip}");
			
			echo '<form action = "ReviewAgency.php" method = "post" name = "agency_by_zip">';
            echo '<table id = "displayAgency" class = "donationtable">';
                        
            while($row = mysql_fetch_array($result)){

	            echo <<<EPRINT
	                 <tr><p>
	                 <td><input type = 'radio' name = 'agency_search' value = '{$row['ID']}'> </td> <td> {$row['Name']} </td>
	                 </p></tr>
                            
EPRINT;
                                                        
            }
            echo <<<EPRINT
            </table>
            <p><br/><br/>
            <input type = 'submit' name = 'submit' value = 'Review Agency Information'>
            </p>
        	
            </form>
            <p><br/><input type = 'submit' name = 'submit' value = 'Back' onclick='history.go(-1);'></p>
                            
EPRINT;
                          
			mysql_close($con);
			
		}
		
	// Display all the agency for a given city name entered by the Donor.
	public function display_by_city(){
			
			echo "<h2><br/> List of Agency By City </h2>";
			$con = mysql_connect('localhost','pa_user','secret_password');
			if (!$con){
		  		die('Could not connect: ' . mysql_error());
		  	}
		  
			mysql_select_db("TGS_GM_RESERVATION", $con);
		
			$city = $_POST['city'];
		
			$result = mysql_query("SELECT distinct ID, Name FROM TGS_GM_RESERVATION.tblagency where City = '{$city}'");
			
			echo '<form action = "ReviewAgency.php" method = "post" name = "agency_by_city">';
            echo '<table id = "displayAgency">';
                        
            while($row = mysql_fetch_array($result)){

                  echo <<<EPRINT
                        <tr><p>
                        <td><input type = 'radio' name = 'agency_search' value = '{$row['ID']}'> </td> <td> {$row['Name']} </td>
                        </p></tr>
                            
EPRINT;
                                                        
                        }
             echo <<<EPRINT
             </table>
             <p><br/><br/>
             <input type = 'submit' name = 'submit' value = 'Review Agency Information'>
             </p>
             </form>
             <p><br/><input type = 'submit' name = 'submit' value = 'Back' onclick='history.go(-1);'></p>
             
                            
EPRINT;
                          
			mysql_close($con);
			
		}

		// Display all the agency for a given Service category entered by the Donor.
		public function display_by_servcat(){
			
			echo "<h2><br/> List of Agency By Service Category </h2>";
			$con = mysql_connect('localhost','pa_user','secret_password');
			if (!$con){
		  		die('Could not connect: ' . mysql_error());
		  	}
		  
			mysql_select_db("TGS_GM_RESERVATION", $con);
		
			$servcat = $_POST['servcat'];
		
			$result = mysql_query("SELECT distinct ID, Name FROM TGS_GM_RESERVATION.tblagency WHERE ID IN (SELECT AgencyID FROM TGS_GM_RESERVATION.tblagencycategory WHERE AgencyCategoryID IN (SELECT CategoryID FROM TGS_GM_RESERVATION.tblcategory WHERE Description = '{$servcat}'))");
			
			echo '<form action = "ReviewAgency.php" method = "post" name = "agency_by_city">';
            echo '<table id = "displayAgency">';
                        
            while($row = mysql_fetch_array($result)){

                  echo <<<EPRINT
                  <tr><p>
                  <td><input type = 'radio' name = 'agency_search' value = '{$row['ID']}'> </td> <td> {$row['Name']} </td>
                  </p></tr>
                            
EPRINT;
                                                        
            }
             echo <<<EPRINT
             </table>
             <p><br/><br/>
             <input type = 'submit' name = 'submit' value = 'Review Agency Information'>
             </p>
        	 </form>
             <p><br/><input type = 'submit' name = 'submit' value = 'Back' onclick='history.go(-1);'></p>
                            
EPRINT;
                          
			mysql_close($con);
			
		}
	
	// Display all the agency for the Neighbourhood entered by the Donor.
	public function display_by_neighbour(){
			
			echo "<h2><br/> List of Agency By Neighbourhood </h2>";
			$con = mysql_connect('localhost','pa_user','secret_password');
			if (!$con){
		  		die('Could not connect: ' . mysql_error());
		  	}
		  
			mysql_select_db("TGS_GM_RESERVATION", $con);
		
			$neighbour = $_POST['neighbour'];
		
			$result = mysql_query("SELECT distinct ID, Name FROM TGS_GM_RESERVATION.tblagency WHERE ID IN (SELECT AgencyID FROM TGS_GM_RESERVATION.tblagencycategory WHERE VenueNeighborhoodCode IN (SELECT NeighborhoodCode FROM TGS_GM_RESERVATION.lkpneighborhoods WHERE NeighborhoodDescr = '{$neighbour}'))");
			
			echo '<form action = "ReviewAgency.php" method = "post" name = "agency_by_city">';
            echo '<table id = "displayAgency">';
                        
            while($row = mysql_fetch_array($result)){

                 echo <<<EPRINT
                        <tr><p>
                        <td><input type = 'radio' name = 'agency_search' value = '{$row['ID']}'> </td> <td> {$row['Name']} </td>
                        </p></tr>
                            
EPRINT;
            }
            echo <<<EPRINT
            </table>
            <p><br/><br/>
            <input type = 'submit' name = 'submit' value = 'Review Agency Information'>
            </p>
            </form>
			<p><br/><input type = 'submit' name = 'submit' value = 'Back' onclick='history.go(-1);'></p>
            
EPRINT;
                          
			mysql_close($con);
			
		}
}

// Session id used to verify if the payment has been made or not. Set to false if user makes use of the Back button.
session_start();
$_SESSION['payment_made'] = 'false';


$page = new DisplayAgency();
$page->header();
$page->top_menu_donor();

if($_POST['zipcode'] != NULL)
	$page->display_by_zip();
else if($_POST['city'] != '0')
	$page->display_by_city();
else if($_POST['neighbour'] != '0')
	$page->display_by_neighbour();
else if($_POST['servcat'] != '0')
	$page->display_by_servcat();
		
$page->footer();

?>